
'use strict';

const Controller = require('egg').Controller;

class DefaultController extends Controller {

  async login() {
    const { ctx, app } = this;
    const userList = await app.redis.get('userList');
    let OutPutList = [];
    if (userList) {
      OutPutList = userList.split(',');
    }
    OutPutList = [ ...new Set(OutPutList) ];
    ctx.socket.emit('login', { userList: OutPutList });
  }


  async toone() {
    const { ctx, app } = this;
    const nsp = app.io.of('/');
    const { from, to, message } = ctx.args[0].data;
    if (to) {
      const uid = await app.redis.get(to);
      console.info(uid);
      // nsp.sockets[uid].emit('onemsg', ctx.args[0].data);
      ctx.socket.to(uid).emit('onemsg', ctx.args[0].data);
    }
  }

  async vuelogin() {
    const { ctx, app } = this;
    const { username } = ctx.args[0];
    await app.redis.set(username, ctx.socket.id);
    const userList = await app.redis.get('userList');
    let OutPutList = [];
    if (userList) {
      OutPutList = userList.split(',');
    }
    OutPutList.push(username);
    OutPutList = [ ...new Set(OutPutList) ];
    await app.redis.set('userList', OutPutList.join());
    ctx.socket.emit('vlogin', { userList: OutPutList });
  }

  async vsendmeg() {
    const { ctx, app } = this;
    const nsp = app.io.of('/');
    const { from, to, message } = ctx.args[0];
    const tofrom = await ctx.service.common.find(from);
    const touser = await ctx.service.common.find(to);
    ctx.args[0].tofrom = tofrom;
    ctx.args[0].touser = touser;
    const uid = await app.redis.get(to);
    ctx.socket.to(uid).emit('toone', ctx.args[0]);
    await ctx.service.common.setsocketim(ctx.args[0]);
  }
}

module.exports = DefaultController;
